package com.kedacom.freedomLabel.view.fragment.setting;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.databinding.FragmentCommunicationSettingBinding;
import com.kedacom.freedomLabel.utils.IPUtil;
import com.kedacom.freedomLabel.view.fragment.SettingFragmentsControl;
import com.kedacom.freedomLabel.viewmodel.CommunicationSettingViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.OnMessage;
import com.kedacom.lego.annotation.ViewModel;
import com.kedacom.lego.util.Utils;


@ContentView(R.layout.fragment_communication_setting)
@ViewModel(CommunicationSettingViewModel.class)
public class CommunicationSettingFragment extends ASettingFragment<FragmentCommunicationSettingBinding, CommunicationSettingViewModel> {

    String localIpStr;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        localIpStr = IPUtil.getLocalIpStr(getActivity());

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View root = super.onCreateView(inflater, container, savedInstanceState);
        mBinding.scalePortZero.setText(PreferenceManager.getDefaultSharedPreferences(getContext()).getString("alarm_serial_port", "/dev/ttymxc3"));
        mBinding.scalePortZero.setOnClickListener(v ->
        {
            beep();
            if (getActivity() instanceof SettingFragmentsControl.ChildSettingFragmentClickCallback) {
                ((SettingFragmentsControl.ChildSettingFragmentClickCallback) getActivity())
                        .onChildSettingFragmentItemClick(SettingFragmentsControl.Intention.OPEN_ALARM_SERIAL_TEST);
            }
        });
        mBinding.scalePortOne.setText("关 9600 8 1 无");
        mBinding.scalePortTwo.setText("连续发送 9600 8 1 无");

        mBinding.scalesServerPort.setText(localIpStr + ":8080");
        mBinding.scalesServerPortEhr.setText(localIpStr);
        mBinding.alarmPortSetting.setText(PreferenceManager.getDefaultSharedPreferences(getContext()).getString("alarm_serial_port_com", "/dev/ttymxc3"));
        mBinding.alarmPortSetting.setOnClickListener(v -> {
            beep();
            if (getActivity() instanceof SettingFragmentsControl.ChildSettingFragmentClickCallback) {
                ((SettingFragmentsControl.ChildSettingFragmentClickCallback) getActivity())
                        .onChildSettingFragmentItemClick(SettingFragmentsControl.Intention.OPEN_ALARM_SERIAL_TEST_COM);
            }
        });
        return root;
    }

    @OnMessage("CommunicationSettingFragment_updateAlarmPort")
    public void updateAlarmPort(String serialString) {
        mBinding.scalePortZero.setText(serialString);
    }
    @OnMessage("CommunicationSettingFragment_updateAlarmPort_com")
    public void updateAlarmPortCOM(String serialString) {
        mBinding.alarmPortSetting.setText(serialString);
    }

    @Override
    public void saveData() {

    }

    @Override
    public String getFragmentTitle() {
        return Utils.getApp().getString(R.string.setting_frag_name_communication);
    }
}